<template>
  <el-container class="container">
    <el-header>
      <div>
        <img src="../assets/logo.png" width="35px"/>
        <span :style="{fontSize: '18px'}">电商后台管理系统</span>
      </div>
      <el-button type="info" @click="quit">退出</el-button>
    </el-header>
    <el-container class="body_container">
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse? '60px':'220px'">
        <div class="toggle-button" @click="toggleCollapse">| | |</div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#6197f9"
          text-color="#fff"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]" :style="{marginRight: '20px', color: '#fff'}"></i>
              <span :style="{fontSize: '16px'}">{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subitem.path + ''"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="saveNavState('/'+subitem.path)"
            >
              <i class="el-icon-menu" :style="{color: '#fff'}"></i>
              <span>{{subitem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data() {
    return {
      menulist: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  methods: {
    quit() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(res)
    },
    // 监听|||按钮的点击来控制左侧边栏收缩与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}

.el-header {
  background-color: #6197f9;
  color: #fff;
  text-align: center;
  line-height: 60px;
  div {
    display: flex;
    justify-content: center;
    float: left;
    height: 100%;
    span {
      margin-left: 10px;
    }
  }
  button {
    float: right;
    margin: 10px;
  }
}

.body_container {
  height: 100%;
}

.el-aside {
  background-color: #6197f9;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
}

.toggle-button {
  height: 50px;
  color: #fff;
  font-size: 20px;
  line-height: 50px;
  text-align: center;
  background-color: #7da4f3;
  cursor: pointer;
}
.toggle-button:hover {
  background-color: #4a8cf5;
}
</style>
